Page({
  data: {
    partKeyword:[],
    part:[],
    currentIndex:0
  },
  onLoad(e){
    if(e.part === '头部'){
      this.setData({
        part:[e.part,'五官']
      })
    }else{
      this.setData({
        part:[e.part]
      })
    }
  },
  onShow() {
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    wx.cloud.callFunction({
      name:'getKeyword',
      data:{
        part:this.data.part[this.data.currentIndex]
      }
    }).then(res =>{
      this.setData({
        partKeyword:res.result.data
      })
      wx.hideLoading()
    }).catch(err => console.log('云函数接口调用失败',err))
  },
  // 点击部位
  handleItem(e){
    if(this.data.currentIndex === e.currentTarget.dataset.index){
      return
    }else{
      let {index} = e.currentTarget.dataset
      this.setData({
        currentIndex:index
      })
      const part = this.data.part[index]
      wx.showLoading({
        title: '加载中',
        mask:true
      })
      wx.cloud.callFunction({
        name:'getKeyword',
        data:{ part }
      }).then(res =>{
        this.setData({
          partKeyword:res.result.data
        })
        wx.hideLoading()
      }).catch(err => console.log('云函数调用获取失败',err) )
    }
  },
  // 点击关键字
  handleKeyword(e){
    let {index} = e.currentTarget.dataset
    let keyword = this.data.partKeyword[index]
    const part = this.data.part[this.data.currentIndex]
    wx.navigateTo({
      url: '/pages/more_keyword/more_keyword?part='+part+'&keyword='+keyword,
    })
  }
})